package demo.hms.user.controller;


import demo.hms.mybatis.entity.User;
import demo.hms.user.service.UserService;
import demo.hms.util.Result;
import demo.hms.util.ResultSubmit;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

@ResponseBody
@Controller
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public Result login(@RequestBody Map<String, String> map, HttpServletRequest request) {
        String username = map.get("userId");
        String possword = map.get("userPassword");
        if (username == null || username.equals("") || possword == null || possword.equals("")) {
            return ResultSubmit.error("输入参数不对");
        }
        return userService.login(username, possword, request);
    }

    @RequestMapping(value = "login2", method = RequestMethod.GET)
    public Result login2(String userId, String password, HttpServletRequest request) {
        return userService.login(userId, password, request);

    }

    @RequestMapping(value = "/info", method = RequestMethod.GET)
    public Result info(HttpServletRequest request) {
        return userService.info(request);
    }


    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    public Result logout(HttpServletRequest request) {
        HttpSession session = request.getSession();
        session.setAttribute("userId",null);
        session.setAttribute("userName",null);
        return ResultSubmit.success("退出成功");
    }

   @RequestMapping(value = "/selectAllUser", method = RequestMethod.GET)
    public Result select( String userId,String userName,String roleName) {
        return  userService.selectAllUser(userId,userName,roleName);
    }

    @RequestMapping(value = "/insertUser", method = RequestMethod.GET)
    public Result insert(User user) {
        return  userService.insertUser(user);
    }

    @RequestMapping(value = "/updateUser", method = RequestMethod.GET)
    public Result update(User user) {
        return  userService.updateUser(user);
    }

    @RequestMapping(value = "/deleteUser", method = RequestMethod.GET)
    public Result dabbleUser(String userId) {
        return  userService.dabbleUser(userId);
    }


}
